<?php
/**
 * Created by PhpStorm.
 * User: ASUS
 * Date: 2019/3/5
 * Time: 10:01
 */

namespace App\Services\Usermanagement;

use App\Model\User;
use App\Services\CommonService;
class UsermanagementService extends CommonService
{
    public function UserLogin($data)
    {
        $res=User::where("u_phone","=",$data['phone'])
            ->get()
            ->toArray();
        if($res[0]['u_password']!==$data['password']){
            return $this->fail(213,'密码不正确');
        }

        $token=md5($res[0]['id'].$res[0]['u_phone'].$res[0]['u_password'].time());
        unset($res[0]['u_password']);
        $redis=new \redis();//实例化redis
        $redis->connect("127.0.0.1",6379);//连接token
        if($tokenName=$redis->get($data['phone']))  //看看用户有没有token
        {
            $redis->delete($tokenName);   //删除以前的token
        }
        $result=$redis->set($token,json_encode($res),25200); //设置token
        $isok=$redis->set($data['phone'],$token,25200);
        if($result&&$isok)
        {
            $array['token']=$token;
            $array['type']=2;
            return $this->success($array);
        }
        else
        {
            $redis->delete($token,$data['phone']);
            return  $this->fail(214,'设置token失败');
        }
    }

    public function getToken($token)
    {
        return $this->CheckToken($token);
    }
}